package com.itheima.controller;

import com.itheima.pojo.SysDictData;
import com.itheima.service.SysDictDataService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.SysDictDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/sysDictData")
@CrossOrigin
public class SysDictDataController {

    @Autowired
    private SysDictDataService sysDictDataService;

    /**
     * 添加字典数据
     *@param sysDictDataVO
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/add")
    public String add(@RequestBody SysDictDataVO sysDictDataVO) {
        try {
            sysDictDataService.add(sysDictDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED, MessageConstant.DICTDATA_ADD_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_OK, MessageConstant.DICTDATA_ADD_SUCCESS);
    }


    /**
     * 批量删除字典数据
     *@param sysDictDataVOList
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/deleteBatch")
    public String  deleteBatch(@RequestBody List<SysDictDataVO> sysDictDataVOList) {
        try {
            sysDictDataService.deleteBatch(sysDictDataVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED,MessageConstant.DICTDATA_delete_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_OK,MessageConstant.DICTDATA_DELETE_SUCCESS);
    }

    /**
     * 编辑字典数据成功
     *@param sysDictDataVO
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/edit")
    public String edit(@RequestBody SysDictDataVO sysDictDataVO) {
        try {
            sysDictDataService.edit(sysDictDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED,MessageConstant.DICTDATA_EDIT_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_OK,MessageConstant.DICTDATA_EDIT_SUCCESS);
    }

    /**
     * 获取字典数据列表
     *@param sysDictDataVO
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/getList")
    public String getList(@RequestBody SysDictDataVO sysDictDataVO) {
        List<SysDictData> sysDictDataList = null;
        try {
            sysDictDataList = sysDictDataService.getList(sysDictDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED,MessageConstant.DICTDATA_FINDALL_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_OK,sysDictDataList);
    }

    /**
     * 根据字典类型获取字典数据
     *@param dictType
     * @return com.itheima.entity.Result
     *@author itheima
     */
    /*存在BUG*/
    @PostMapping("/getListByDictType")
    public String getListByDictType(@RequestParam String dictType) {
        List<SysDictData> sysDictDataList = sysDictDataService.getListByDictType(dictType);
        return ResultUtil.result(HttpServletResponse.SC_OK,sysDictDataList);
    }

    /**
     * 根据字典类型数组获取字典数据
     *@param dictTypeList
     * @return com.itheima.entity.Result
     *@author itheima
     */
    /*未测*/
    @PostMapping("/getListByDictTypeList")
    public String getListByDictTypeList(@RequestBody List<String> dictTypeList) {
        List<SysDictData> sysDictDatas = sysDictDataService.getListByDictTypeList(dictTypeList);
        return ResultUtil.result(HttpServletResponse.SC_OK,sysDictDatas);
    }

}
